package com.liuxinlong.modules.system.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.butler.controller.request.QueryCommentReq;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Expert;
import com.liuxinlong.modules.system.controller.request.UpdateExpertReq;
import com.liuxinlong.modules.system.service.ExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专家管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-12-08
 */
@RestController
@RequestMapping("/expert")
public class ExpertController {

    @Autowired
    private ExpertService expertService;

    /**
     * 分页查询专家信息
     *
     * @param req 请求信息
     * @return 专家信息列表
     */
    @PostMapping("page")
    public BaseResp pageFaqInfo(@RequestBody QueryCommentReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("keyword", req.getKeyword());
        List<Map<String, Object>> dataList = expertService.pageExpertInfo(queryMap);
        int totalNum = expertService.getExpertInfoCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询专家信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增专家信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增专家信息", business = LogBusinessConstants.EXPERT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp addExpert(@RequestBody UpdateExpertReq req) {

        Expert expert = new Expert();
        expert.setId(req.getId());
        expert.setArea(req.getArea());
        expert.setBrief(req.getBrief());
        expert.setPicture(req.getPicture());
        expertService.addExpertInfo(expert);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增专家信息成功");
        return resp;
    }

    /**
     * 修改专家信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改专家信息", business = LogBusinessConstants.EXPERT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateExpert(@RequestBody UpdateExpertReq req) {

        Expert expert = new Expert();
        expert.setId(req.getId());
        expert.setArea(req.getArea());
        expert.setBrief(req.getBrief());
        expert.setPicture(req.getPicture());
        expertService.updateExpert(expert);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改专家信息成功");
        return resp;
    }

    /**
     * 删除专家信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除专家信息", business = LogBusinessConstants.EXPERT_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteExpert(@RequestBody BaseDeleteReq req) {
        expertService.deleteExpert(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除专家信息成功");
        return resp;
    }

    /**
     * 导出专家信息
     *
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value ="导出专家信息",business = LogBusinessConstants.EXPERT_MANAGE,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportExpert(HttpServletResponse response) {
        expertService.exportExpert(response);
    }

    /**
     * 导出专家贡献信息
     *
     * @return 导出数据
     */
    @PostMapping("contribution-export")
    @LogAnnotation(value ="导出专家贡献信息",business = LogBusinessConstants.EXPERT_MANAGE,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportContribution(HttpServletResponse response) {
        expertService.exportContribution(response);
    }

    /**
     * 统计数据校正
     *
     * @return 成功标识
     */
    @GetMapping("sync")
    @LogAnnotation(value = "统计数据校正", business = LogBusinessConstants.EXPERT_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp syncStatisticalData() {
        int total = expertService.syncStatisticalData();
        BaseResp resp = new BaseResp();
        resp.setRetMessage("统计数据校正成功！");
        resp.setTotalNum(total);
        return resp;
    }
}
